package com.hbter630.change.controller;

import cn.hutool.core.util.RandomUtil;
import com.hbter630.change.entity.User;
import com.hbter630.change.service.UserService;
import com.hbter630.change.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Title: EmailController
 * @Author 笑霸fianl
 * @Package com.hbter630.change.controller
 * @Date 2023/5/30 9:04
 * @描述: 邮箱发送
 */
@RequestMapping("api/email")
@RestController
@Slf4j
@Api(tags = "邮箱配置")
public class EmailController {

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @Resource
    private JavaMailSender mailSender;

    @Resource
    private UserService userService;

    @Value("${spring.mail.username}")
    private String MyFrom;

    /**
     * 发送验证码
     * @param email
     * @return
     */
    @ApiOperation(notes = "发送邮件",value = "发送邮件", httpMethod = "GET", response = Result.class)
    @GetMapping("/sendMsg/{email}")
    public Result sendMsg(@PathVariable("email") String email){
        log.info("R<String> sendMsg()进来了{}",email);

        //判断邮件不为空
        if(StringUtils.isNotEmpty(email)) {
            int code = RandomUtil.randomInt(1000,9999);

            System.out.println("==========");
            System.out.println(code);//验证码
            System.out.println("==========");

            //***************************************************/
            //创建简单邮件消息
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(MyFrom);//用自己的邮件发
            //谁要接收
            message.setTo(email);
            //邮件标题
            message.setSubject("验证码");
            //邮件内容
            message.setText("你的验证码是："+code+"\n欢迎注册随手换");//
            try {
                mailSender.send(message);
                //需要保存一下验证码，后面用来验证
                  redisTemplate.opsForValue().set(email,email+code,5, TimeUnit.MINUTES);

                return Result.ok().addData(Result.ok());
            } catch (MailException e) {
                e.printStackTrace();
                return Result.failed("短信发送失败");
            }
        }
        return Result.failed("短信发送失败");
    }

    @ApiOperation(notes = "验证邮件",value = "验证邮件", httpMethod = "GET", response = Result.class)
    @GetMapping("/check/{email}/{code}")
    public Result check(@PathVariable("email") String email,@PathVariable("code") String code){

        //取验证码
        String s = redisTemplate.opsForValue().get(email);
        log.info("R<String> 验证邮件()进来了{}======={}======{}",email,code,s);
        if(StringUtils.isNotEmpty(email) && StringUtils.isNotEmpty(code)){

            if((email+code).equals(s)){
                //成功
                redisTemplate.delete(email);
                return  Result.ok().addData(Result.ok());
            }

        }
        return  Result.failed();

    }








}
